using System;
using System.Collections.Generic;
using System.Linq;

namespace CF_DAL_WS.Repositories
{
	#region using
	using CF_DAL_WS.Models;
	using CF_DAL_WS.Helpers;
	#endregion
	public class OrdersRepository
	{
		public List<Orders> SelectAll()
		{
			try
			{
				return DataContext.Instance.Orders.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Orders>();
		}

		public Orders SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.Orders.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public int Insert(Orders obj)
		{
			try
			{
				DataContext.Instance.Orders.AddObject(obj);
				DataContext.Instance.SaveChanges();
                return obj.ID;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return -1;// Cannot insert
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.Orders.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.Orders.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<Orders> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.Orders where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Orders>();
		}

		public List<Orders> SelectByIDUsers(Guid iduser)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.Orders where item.IDUser == iduser select item).OrderByDescending(n => n.OrderDate).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Orders>();
		}

        public List<Orders> SelectByIDRestaurant(int idrestaurant)
        {
            try
            {
                var lstItem = (from item in DataContext.Instance.Orders where item.IDRestaurant == idrestaurant select item).OrderByDescending(n => n.OrderDate).ToList();
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<Orders>();
        }
	}
}